package com.zhao.service.serviceImpl;

import com.sun.org.apache.xpath.internal.operations.Or;
import com.zhao.Others.DateAndTimeObject;
import com.zhao.Others.GenerateObject;
import com.zhao.dao.EbickDao;
import com.zhao.dao.EbickGoodsDao;
import com.zhao.dao.OrderFormDao;
import com.zhao.dao.StudentDao;
import com.zhao.pojo.*;
import com.zhao.service.EbickGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class EbickGoodsServiceImpl implements EbickGoodsService {

    @Autowired
    private EbickDao ebickDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private EbickGoodsDao ebickGoodsDao;

    @Autowired
    private OrderFormDao orderFormDao;

    @Override
    public String studentUploadEbickGoods(Student student, EbickGoods ebickGoods, List<String> imagePathList ,HttpSession session) {

        Ebick ebick = ebickDao.findByID(ebickGoods.getEbickID(),student.getUniversityID());
        if(ebick == null || !ebick.getNowOwnerID().equals(student.getStudentID())){
            return "电动车牌照错误";
        }
        if(ebick.getStatus().equals("申请行驶证明待批准")){
            return "该电车正在申请行驶证";
        }
        if(ebick.getStatus().equals("有证")){
            return "请先注销行驶证";
        }
        EbickGoods ebickGoods1 = ebickGoodsDao.findByEbickID(ebickGoods.getEbickID(),student.getUniversityID());
        if(ebickGoods1 != null){
            return "该电车已上传";
        }

        ebickGoods.setReleaseTime(DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());
        ebickGoods.setStatus("已发布");
        ebickGoods.setUniversityID(student.getUniversityID());
        ebickGoods.setStudentID(student.getStudentID());

        String image = "";
        for(String path : imagePathList){
            image+=path+";";
        }
        ebickGoods.setImages(image);
        System.out.println(ebickGoods);

        int res = ebickGoodsDao.addOne(ebickGoods);
        if(res!=1){
            return "添加添加失败";
        }

        ebick.setStatus("已发布");
        ebickDao.updateStatus(ebick);

        return "success";
    }

    private EbickGoodsImpl ebickgoodsToEbickGoodsImpl(EbickGoods ebickGoods,Student student,Ebick ebick){
        EbickGoodsImpl ebickGoods1 = new EbickGoodsImpl();
        ebickGoods1.setEbickGoods(ebickGoods);
        ebickGoods1.setStudent(student);
        ebickGoods1.setEbick(ebick);
        String[] images = ebickGoods.getImages().split(";");
        List<String> imagesList = new ArrayList<>();
        for(String image : images){
            if(image.length()>0){
                imagesList.add(image);
            }
        }
        String[] lables = ebickGoods.getLabels().split("#");
        List<String> lablesList = new ArrayList<>();
        for(String lable : lables){
            if(lable.length()>0){
                lablesList.add("#"+lable);
            }
        }
        ebickGoods1.setImagesList(imagesList);
        ebickGoods1.setLabelsList(lablesList);
        return ebickGoods1;
    }

    @Override
    public String studentGetAllMyEbickGoods(Student student, HttpSession session) {
        List<EbickGoods> ebickGoodsList = ebickGoodsDao.findByStudentIDAndNotStatus("已完成",student.getStudentID(),student.getUniversityID());
        //System.out.println(student);
        //System.out.println(ebickGoodsList);
        Student student1 = studentDao.findByStudentID(student.getStudentID(),student.getUniversityID());
        List<EbickGoodsImpl> ebickGoodsImplList = new ArrayList<>();
        for(EbickGoods ebickGoods : ebickGoodsList){
            Ebick ebick = ebickDao.findByID(ebickGoods.getEbickID(),ebickGoods.getUniversityID());
            ebickGoodsImplList.add(ebickgoodsToEbickGoodsImpl(ebickGoods,student,ebick));
        }
        session.setAttribute("allMyEbickGoodsImplList",ebickGoodsImplList);
        System.out.println(ebickGoodsImplList);
        return "success";
    }

    @Override
    public String studentToUpdateEbickGoods(Student student, String ebickID, HttpSession session) {

        EbickGoods ebickGoods = ebickGoodsDao.findByEbickID(ebickID,student.getUniversityID());
        if(ebickGoods == null){
            return "error";
        }
        session.setAttribute("needUpdateEbickGoods",ebickGoods);

        return "success";
    }

    @Override
    public String studentDeleteEbickGoods(Student student, String ebickID, HttpSession session) {
        Ebick ebick = ebickDao.findByID(ebickID,student.getUniversityID());
        ebick.setStatus("无证");
        int res = ebickDao.updateStatus(ebick);
        if(res!=1){
            return "error";
        }
        res = ebickGoodsDao.delete(ebickID,student.getUniversityID());
        if(res!=1){
            return "error";
        }
        return "success";
    }

    @Override
    public String studentUpdateEbickGoods(Student student, EbickGoods ebickGoods, HttpSession session) {

        int res = ebickGoodsDao.update(ebickGoods);
        if(res!=1){
            return "更新失败";
        }
        return "success";
    }

    @Override
    public String studentSeeAllEbickGoods(Student student, HttpSession session) {

        List<EbickGoods> ebickGoodsList = ebickGoodsDao.findAllByNotStatus("已完成",student.getUniversityID());
        List<EbickGoodsImpl> ebickGoodsImplList = new ArrayList<>();
        for(EbickGoods ebickGoods : ebickGoodsList){
            Student student1 = studentDao.findByStudentID(ebickGoods.getStudentID(),ebickGoods.getUniversityID());
            Ebick ebick = ebickDao.findByID(ebickGoods.getEbickID(),ebickGoods.getUniversityID());
            ebickGoodsImplList.add(ebickgoodsToEbickGoodsImpl(ebickGoods,student1,ebick));
        }
        session.setAttribute("allEbickGoodsImplList",ebickGoodsImplList);


        return "success";
    }

    @Override
    public String getEbickGoodsNum(String universityID, HttpSession session) {

        List<EbickGoods> ebickGoodsList = ebickGoodsDao.findAllByStatus("已完成",universityID);
        int ebickGoodsNum = ebickGoodsList.size();
        session.setAttribute("ebickGoodsNum",ebickGoodsNum);

        return "success";
    }


}
